import axios from 'axios'
import axiosFetch from '../../fetch/axios'

const state = {
    goodList: [],
    exList: []
}


const mutations = {
    setGoodList(state, data) {
        state.goodList = data
    },
    setExList(state, data) {
        state.exList = data
    }
}

const actions = {
    getGoodList({ commit }, parmas) {
        return new Promise((resolve, reject) => {
            axios.get('/ex/goodlist', {}).then(function(response) {　
                commit('setGoodList', response.data);
            }).catch(function(error) {　　
                console.log(error);　
            });

        })
    },
    saveChangeLog({ commit }, params) {
        return new Promise((resolve, reject) => {
            axiosFetch.fetchPost('/ex/addchangelog', params).then(responseData => {
                return resolve(responseData);
            }, err => {
                return reject(err)
            });
        })
    },
    getAddressInfo({ commit }, params) {
        return new Promise((resolve, reject) => {
            axiosFetch.fetchPost('/ex/getaddressinfo', params).then(responseData => {
                return resolve(responseData);
            }, err => {
                return reject(err)
            });
        })
    },
    getUserInfo({ commit }, parmas) {
        return new Promise((resolve, reject) => {
            axios.get('/user/getuserinfo/' + parmas, {}).then(function(response) {　
                return resolve(response);
            }).catch(function(error) {　　
                return reject(error)
            });

        })
    },
    checkUserInfo({ commit }, parmas) {
        return new Promise((resolve, reject) => {
            axios.get('/user/check', { params: { 'openid': parmas } }).then(function(response) {　
                return resolve(response);
            }).catch(function(error) {　　
                return reject(error)
            });
        })
    },
    getExList({ commit }, parmas) {
        return new Promise((resolve, reject) => {
            axios.get('/ex/list', {}).then(function(response) {　
                commit('setExList', response.data);
                return resolve(response);
            }).catch(function(error) {　　
                console.log(error);　
            });

        })
    },
    addExLog({ commit }, parmas) {
        return new Promise((resolve, reject) => {
            axiosFetch.fetchPost('/ex/addexlog', parmas).then(responseData => {
                return resolve(responseData);
            }, err => {
                return reject(err)
            });
        })
    },
    getTodayTimes({commit},parmas){
        return new Promise((resolve, reject) => {
            axios.get("/ex/GetTodayUserExLogTimes", { params: { 'openid': parmas } }).then(function(response) {　
                return resolve(response);
            }).catch(function(error) {　　
                return reject(error)
            });
        })
    }
}


const getters = {
    goodList: state => state.goodList,
    exList: state => state.exList
}

export default {
    state,
    mutations,
    actions,
    getters
}